<?

$dataup = 0;
$uprun  = true;

if($_POST["entry"]){

  $Db = new oracleDb;
  $Db->init($_SESSION["ORCL"],
             $_SESSION["OPASS"],
             $_SESSION["SID"],
             _NO_COMMENT);

  /**
  * Check the entry
  *
  */
  for($iRow = 0; $iRow < $_SESSION["init_num"]; $iRow++){

    /**
    * Check for each master detail
    *
    */
    if($_POST["snEnt"][$iRow] == '' ){

      for($iNext = ($iRow + 1); $iNext < $_SESSION["init_num"]; $iNext++ ){

        if($_POST["snEnt"][$iNext] != '' ){

          $uprun = false;

          $Misc->usrError("Invalid Entry at : ".($iNext + ARRAY_NUMBER)." ");

          break;
        }
      }

      if($uprun == false){

        /* out of this loop */
        break;
      }
    }else{

      if($_POST["snEnt"][$iRow] != ''){

        if($_POST["snEnt"][$iRow]){

          /**
           * check out the validity
           */
          $CFetchers->fetchWhsHasSn($Db,
                                    $_POST["fromEnt"],
                                    $_POST["snEnt"][$iRow],
                                    $SourceDb->fetchValue("srcDbSubDtl"),
                                    $retSN);

        }else{

          $retSN = false;
        }

        if(!$retSN){

          $dataup++;
        }else{

          $uprun = false;

          $Misc->usrError("Invalid Entry at : ".($iRow + ARRAY_NUMBER)." <br/>
                           ".$retSN." doesnt exists in ".$_POST["fromEnt"]." ");
        }
      }else{

        $uprun = false;

        $Misc->usrError("Invalid Entry at : ".($iRow + ARRAY_NUMBER)." ");
      }
    }
  }
}else{

  /**
  * Check the entry
  *
  */
  for($iRow = 0; $iRow < $_SESSION["init_num"]; $iRow++){

    /**
    * Check for each master detail
    *
    */
    if($_POST["snEnt"][$iRow] == '' ){

      for($iNext = ($iRow + 1); $iNext < $_SESSION["init_num"]; $iNext++ ){

        if($_POST["snEnt"][$iNext] != '' ){

          $uprun = false;

          $Misc->usrError("Invalid Entry at : ".($iNext + ARRAY_NUMBER)." ");

          break;
        }
      }

      if($uprun == false){

        /* out of this loop */
        break;
      }
    }else{

      if($_POST["snEnt"][$iRow] != ''){

        $dataup++;
      }else{

        $uprun = false;

        $Misc->usrError("Invalid Entry at : ".($iRow + ARRAY_NUMBER)." ");
      }
    }
  }
}

if($uprun == true){

	/**
	* We are good with details
	* continue with the header check
	*
	*/
	if($_POST["trDateEnt"] != '' &&
	   $_POST["fromEnt"]   != '' &&
	   $_POST["toEnt"]     != '' &&
	   $_POST["modelEnt"]  != '' &&
	   $_POST["qtyEnt"]    != ''){

		//everything is good
		//open confirmation form
		if(!$_POST["confirm"] && !$_POST["back"]){

			require("entry_upload_confirm.php");
		}elseif($_POST["back"]){

			require("entry_restore_me.php");
		}else{

			/**
			* Prepare to upload
			* modify mode is on
			*
			*/
			if($_SESSION["modify_mode"] == "Y" && $_SESSION["jid"] != ''){

				require("upmodifytodb.php");
			}elseif($_SESSION["modify_mode"] != "Y"){ //if modify mode is off

				require("upentrytodb.php");
			}else{

				$Misc->usrError("Unknown Upload Task");
			}
		}
	}else{

		$Misc->usrError("Error: header incompleted");

		require("entry_restore_me.php");
	}
}

if($uprun == false){

	echo "Restoring...\n";
	$restore  = 0;
	$uprun    = 0;
	$dataup   = 0;

	require("entry_restore_me.php");
}
?>